<div v-cloak id="app" class="main-content">
    <one-table
        :option="option"
        :data="data"
        :loading="loading"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
    >
        <template slot="menuLeft">
            <el-button type="primary" @click="getCount">统计</el-button>
        </template>
        <template slot="menuRight">
            <div class="form-box" flex="main:left">
                <el-input
                    type="text"
                    v-model="search.keyword"
                    placeholder="用户ID|页面地址"
                    style="width: 240px; margin-right: 5px"
                ></el-input>
                <el-button type="primary" @click="getData">筛选</el-button>
            </div>
        </template>
        <template slot="status" slot-scope="scope">
            <el-switch
                v-model="scope.row.status"
                :active-value="1"
                :inactive-value="0"
                inactive-text="待处理"
                @change="changeSwitch(scope.row)"
            >
            </el-switch>
        </template>
        <template slot="imgs" slot-scope="scope">
            <div flex="main:center">
                <el-image
                    style="width: 70px; height: 70px"
                    :src="scope.row.imgs"
                    fit="fit"
                ></el-image>
            </div>
        </template>
        <!-- 自定义操作列 -->
        <template slot="operates" slot-scope="scope">
            <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="删除不能恢复,确定删除吗？"
                @confirm="handleDel(scope.row, scope.index)"
            >
                <el-button
                    :disabled="scope.row.system"
                    slot="reference"
                    size="mini"
                    class="_tool"
                    type="danger"
                >
                    <i class="el-icon-delete"></i>
                </el-button>
            </el-popconfirm>
        </template>
    </one-table>

    <el-dialog title="统计信息" width="30%" :visible.sync="dialogTableVisible">
        <div
            style="height: 300px; max-height: 300px; overflow-y: auto"
            v-html="counts"
        ></div>
    </el-dialog>
</div>
{include file='common@components/one-table'}
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                dialogTableVisible: false,
                counts: "",
                loading: false,
                data: [],
                option: {
                    index: false,
                    header: true,
                    selection: false,
                    page: {},
                    size: "mini",
                    column: [
                        {
                            label: "状态",
                            prop: "status",
                            width: 180,
                            slot: true,
                        },
                        {
                            label: "行为类型",
                            prop: "typeText",
                        },
                        {
                            label: "用户ID",
                            prop: "uid",
                        },
                        {
                            label: "详情",
                            prop: "remark",
                        },
                        {
                            label: "首次记录时间",
                            prop: "create_time",
                            width: 160,
                        },
                        {
                            label: "更新时间",
                            prop: "update_time",
                            width: 160,
                        },
                    ],
                },
                search: {
                    keyword: "",
                },
            };
        },

        created() {
            this.getData();
        },
        methods: {
            // 获取数据
            getData() {
                let _this = this;
                _this.loading = true;
                let postData = cloneDeep(_this.search);
                request({
                    params: {
                        s: "user/behavior/index",
                        page: _this.option.page.currentPage,
                        limit: _this.option.page.pageSize,
                    },
                    data: postData,
                    method: "post",
                }).then((e) => {
                    _this.loading = false;
                    _this.data = e.data.data.list;
                    _this.option.page.total = e.data.data.count;
                });
            },
            //分页
            handleCurrentChange(currentPage) {
                let _this = this;
                _this.option.page.currentPage = currentPage;
                _this.getData();
            },
            //每页显示数
            handleSizeChange(val) {
                let _this = this;
                _this.option.page.pageSize = val;
                _this.getData();
            },
            //改变状态
            changeSwitch(row) {
                let _this = this;
                _this.loading = true;
                request({
                    params: {
                        s: "user/behavior/status/",
                        id: row.id,
                        val: row.status,
                    },
                    method: "post",
                }).then((e) => {
                    _this.loading = false;
                    _this.getData();
                });
            },

            //删除行
            handleDel(row, index) {
                let _this = this;
                _this.loading = true;
                request({
                    params: {
                        s: "user/behavior/del/",
                        id: row.id,
                    },
                    method: "post",
                }).then((e) => {
                    _this.data.splice(index, 1);
                    _this.loading = false;
                });
            },
            getCount() {
                let _this = this;
                request({
                    params: {
                        s: "user/behavior/counts/",
                    },
                    method: "post",
                }).then((e) => {
                    this.counts = e.data.data;
                    this.dialogTableVisible = true;
                });
            },
            //添加
            handleAdd() {
                let _this = this;
                navigateTo({
                    s: "user/behavior/edit",
                });
            },
            //编辑
            handleEdit(row, index) {
                let _this = this;
                navigateTo({
                    s: "user/behavior/edit",
                    id: row.id,
                });
            },
        },
    });
</script>
